package com.selfdriving.wl.activity;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.selfdriving.BaseApplication;
import com.selfdriving.R;
import com.selfdriving.activity.FeatureJoinDetails;
import com.selfdriving.adapter.FeatureJoinAdapter;
import com.selfdriving.bean.Entity;
import com.selfdriving.bean.Join;
import com.selfdriving.dialog.ChooseDialog;
import com.selfdriving.utils.Constant;
import com.selfdriving.utils.Utils;
import com.selfdriving.view.MoMoRefreshListView;
import com.selfdriving.view.MoMoRefreshListView.OnCancelListener;
import com.selfdriving.view.MoMoRefreshListView.OnRefreshListener;

public class FeatureJoin implements OnRefreshListener, OnCancelListener
{
	/** 当前View **/
	View view;
	BaseApplication application;
	Context context;
	private TextView tvContactsUs;
	private LinearLayout llContacts;
	private ImageView ivMail;
	private ImageView ivQQ;
	private ImageView ivPhone;
	private ImageView ivArrow;
	private MoMoRefreshListView feature_list;
	public FeatureJoinAdapter adapter;
	private List<Entity> features = new ArrayList<Entity>();
	private View headView;
	private boolean isShow = false;
	private OnClickListener onClickListener;
	private RelativeLayout Link;
	LayoutInflater inflater;
	public int page = 1;

	public FeatureJoin(BaseApplication application, Context context,
			OnClickListener onClickListener)
	{
		inflater = LayoutInflater.from(context);
		view = inflater.inflate(R.layout.refresh_listview, null);
		this.application = application;
		this.context = context;
		this.onClickListener = onClickListener;
		initViews();
		getDatas(false);
	}

	public void initViews()
	{
		feature_list = (MoMoRefreshListView) view
				.findViewById(R.id.refresh_list);
		adapter = new FeatureJoinAdapter(application, context, features);
		headView = inflater.inflate(R.layout.feature_join_header, null);
		headView = inflater.inflate(R.layout.feature_join, null);
		ivMail = (ImageView) headView.findViewById(R.id.ivMail);
		Link = (RelativeLayout) headView.findViewById(R.id.join_link);
		tvContactsUs = (TextView) headView.findViewById(R.id.tvContactsUs);
		llContacts = (LinearLayout) headView.findViewById(R.id.llContacts);
		ivArrow = (ImageView) headView.findViewById(R.id.ivArrow);
		ivQQ = (ImageView) headView.findViewById(R.id.ivQQ);
		ivPhone = (ImageView) headView.findViewById(R.id.ivPhone);
		feature_list.addHeaderView(headView);
		feature_list.setAdapter(adapter);
		// DeviceUtils.setListViewHeightBasedOnChildren(feature_list);
		feature_list.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3)
			{
				if (arg2 > 0)
				{
					Intent intent = new Intent(context,
							FeatureJoinDetails.class);
					context.startActivity(intent);
				}
			}
		});
		feature_list.setOnRefreshListener(this);
		feature_list.setOnCancelListener(this);
		ivMail.setOnClickListener(onClickListener);
		ivPhone.setOnClickListener(onClickListener);
		Link.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0)
			{
				tvContactsUs();
			}
		});

	}

	public void tvContactsUs()
	{
		isShow = !isShow;
		if (isShow)
		{
			llContacts.setVisibility(View.VISIBLE);
			ivArrow.setBackgroundResource(R.drawable.aa_icon_up);
		} else
		{
			llContacts.setVisibility(View.GONE);
			ivArrow.setBackgroundResource(R.drawable.aa_icon_down);
		}
	}

	public View getView()
	{
		return view;
	}

	public void getDatas(final boolean isLoadMost)
	{
		if (!isLoadMost)
		{
			features.clear();
		}
		AjaxParams params = new AjaxParams();
		params.put("f", "79");
		params.put("page", "" + page);
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				Utils.dismissLoadingDialog();
				feature_list.onRefreshComplete();
				feature_list.onLoadMoreComplete(false);
			}

			@Override
			public void onSuccess(Object o)
			{
				String json = o.toString();
				try
				{
					Join join;
					JSONArray array = new JSONArray(json);
					for (int i = 0; i < array.length(); i++)
					{
						JSONObject object = array.getJSONObject(i);
						String id = object.getString("id");
						String city = object.getString("city");
						String img = object.getString("img");
						join = new Join(id, city, img);
						features.add(join);
					}
					feature_list.onRefreshComplete();
					feature_list.onLoadMoreComplete(false);
				} catch (JSONException e)
				{
					feature_list.onRefreshComplete();
					feature_list.onLoadMoreComplete(false);
					e.printStackTrace();
				}
			}
		});
	}

	@Override
	public void onCancel()
	{
		// TODO Auto-generated method stub
		feature_list.onLoadMoreComplete(false);
		feature_list.onRefreshComplete();
	}

	@Override
	public void onRefresh()
	{
		// TODO Auto-generated method stub
		page = 1;
		getDatas(false);
	}

	@Override
	public void onLoadMore()
	{
		// TODO Auto-generated method stub
		getDatas(true);
	}
}
